package org.zhq.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zhq.enums.PaymentStatus;
import org.zhq.enums.YesOrNo;
import org.zhq.mapper.OrdersMapper;
import org.zhq.pojo.Orders;
import org.zhq.pojo.bo.MerchantOrdersBO;
import org.zhq.service.IPaymentOrderService;

import java.util.Date;

@Service
public class PaymentOrderService implements IPaymentOrderService {
    private final OrdersMapper ordersMapper;

    @Autowired
    public PaymentOrderService(OrdersMapper ordersMapper) {
        this.ordersMapper = ordersMapper;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean createPaymentOrder(MerchantOrdersBO merchantOrdersBO) {
        Orders paymentOrder = new Orders();
        BeanUtils.copyProperties(merchantOrdersBO, paymentOrder);
        paymentOrder.setPayStatus(PaymentStatus.WAIT_PAY.type);
        paymentOrder.setComeFrom("天天吃货");
        paymentOrder.setIsDelete(YesOrNo.NO.type);
        paymentOrder.setCreatedTime(new Date());
        int result = ordersMapper.insert(paymentOrder);
        return result == 1;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Orders queryOrderByStatus(String merchantUserId, String merchantOrderId, Integer orderStatus) {
        Wrapper<Orders> condition = Wrappers.<Orders>lambdaQuery()
                .eq(Orders::getMerchantOrderId, merchantOrderId)
                .eq(Orders::getMerchantUserId, merchantUserId)
                .eq(Orders::getPayStatus, orderStatus);
        return ordersMapper.selectOne(condition);
    }

    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public String updateOrderPaid(String merchantOrderId, Integer paidAmount) {
        Orders paidOrder = new Orders();
        paidOrder.setPayStatus(PaymentStatus.PAID.type);
        paidOrder.setAmount(paidAmount);
        LambdaQueryWrapper<Orders> condition = Wrappers.<Orders>lambdaQuery()
                .eq(Orders::getMerchantOrderId, merchantOrderId);
        ordersMapper.update(paidOrder,condition);
        return queryMerchantReturnUrl(merchantOrderId);
    }

    @Transactional(propagation=Propagation.SUPPORTS)
    String queryMerchantReturnUrl(String merchantOrderId) {
        LambdaQueryWrapper<Orders> condition = Wrappers.<Orders>lambdaQuery().eq(Orders::getMerchantOrderId, merchantOrderId);
        Orders order = ordersMapper.selectOne(condition);
        return order.getReturnUrl();
    }

    @Transactional(propagation=Propagation.SUPPORTS)
    @Override
    public Orders queryOrderInfo(String merchantUserId, String merchantOrderId) {
        Orders orderInfo = new Orders();
        orderInfo.setMerchantOrderId(merchantOrderId);
        orderInfo.setMerchantUserId(merchantUserId);
        LambdaQueryWrapper<Orders> condition = Wrappers.<Orders>lambdaQuery()
                .eq(Orders::getMerchantOrderId, merchantOrderId)
                .eq(Orders::getMerchantUserId, merchantUserId);
        return ordersMapper.selectOne(condition);
    }
}
